<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
//for stock

define('COLOR_NAME', 'Farve');
define('SIZE_NAME', 'Størrelse');

ini_set('max_execution_time', 900);
set_time_limit(900);
echo '<pre>';

function group_assoc($array, $key) {
    $return = array();
    foreach($array as $v) {
        $return[$v[$key]][] = $v;
    }
    return $return;
}

function toArray($obj)
{
    if (is_object($obj)) $obj = (array)$obj;
    if (is_array($obj)) {
        $new = array();
        foreach ($obj as $key => $val) {
            $new[$key] = toArray($val);
        }
    } else {
        $new = $obj;
    }
    return $new;
}

function addSizetoproduct($db, $product_id, $quantity, $attribute_id, $arr_size){
	$product_option_row = $db->fetch_row('product_id = ' . $product_id . ", attribute_id = " . $attribute_id, 'product_option', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(empty($exists)){
		$fields = array('product_id','attribute_id');
		$data = array($product_id, $attribute_id);
		$product_option_id = $db->insert($fields, $data, 'product_option');
		
		$fields = array('product_option_id','language_id','product_id','name');
		$data = array($product_option_id, '1', $product_id, SIZE_NAME );
		$db->insert($fields, $data, 'product_option_description');
	}else{
		$product_option_id = $product_option_row[0]['product_option_id'];
	}
	
	foreach($arr_size as $size){
		$attribute_value_id = getAttributeValueId($db, $attribute_id, $size );
		$product_option_value_row = $db->fetch_row('product_option_id = ' . $product_option_id . 
		", product_id = " . $product_id .
		", attribute_id = " .$attribute_id . ", attribute_value_id = " . $attribute_value_id, 'product_option_value', FETCH_ASSOC);
		$exists1 = $db->get_rows();
		if(empty($exists1)){
		
			$attribute_value_id = getAttributeValueId($db, $attribute_id, $size );
			$fields = array('product_option_id','product_id','quantity','attribute_id','attribute_value_id');
			$data = array($product_option_id, $product_id, $quantity , $attribute_id, $attribute_value_id);
			$product_option_value_id = $db->insert($fields, $data, 'product_option_value');
			
			$fields = array('product_option_value_id','language_id','product_id','name');
			$data = array($product_option_value_id, '1', $product_id, $size);
			$db->insert($fields, $data, 'product_option_value_description');
		}
	}
	
	
}

function addSizetoproduct2($db, $product_id, $quantity, $attribute_id, $size){
	$product_option_row = $db->fetch_row('product_id = ' . $product_id . ", attribute_id = " . $attribute_id, 'product_option', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(empty($exists)){
		$fields = array('product_id','attribute_id');
		$data = array($product_id, $attribute_id);
		$product_option_id = $db->insert($fields, $data, 'product_option');
		
		$fields = array('product_option_id','language_id','product_id','name');
		$data = array($product_option_id, '1', $product_id, SIZE_NAME );
		$db->insert($fields, $data, 'product_option_description');
	}else{
		$product_option_id = $product_option_row[0]['product_option_id'];
	}
	

	$attribute_value_id = getAttributeValueId($db, $attribute_id, $size );
	$product_option_value_row = $db->fetch_row('product_option_id = ' . $product_option_id . 
	", product_id = " . $product_id .
	", attribute_id = " .$attribute_id . ", attribute_value_id = " . $attribute_value_id, 'product_option_value', FETCH_ASSOC);
	$exists1 = $db->get_rows();
	if(empty($exists1)){
	
		$attribute_value_id = getAttributeValueId($db, $attribute_id, $size );
		$fields = array('product_option_id','product_id','quantity','attribute_id','attribute_value_id');
		$data = array($product_option_id, $product_id, $quantity , $attribute_id, $attribute_value_id);
		$product_option_value_id = $db->insert($fields, $data, 'product_option_value');
		
		$fields = array('product_option_value_id','language_id','product_id','name');
		$data = array($product_option_value_id, '1', $product_id, $size);
		$db->insert($fields, $data, 'product_option_value_description');
	}
	
	
	
}

function getStockcount($ean, $arrStock){
	$count = '0';
	foreach($arrStock as $key=>$value){
		if($ean == $value['Key']){
			$count = $value['Value'];
			break;
		}
	}
	return $count;
}

function getAttributeId($db, $KatalogNumber, $name){
	$attribute = $db->fetch_row('katalognumber = ' . $KatalogNumber .", name = '" . $name ."'", 'attribute', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(!empty($exists)){
		return $attribute[0]['attribute_id'];
	}
	return '';
}

function getAttributeValueId($db, $attribute_id, $name){
	$attribute_value = $db->fetch_row('attribute_id = ' . $attribute_id .", name = '" . $name ."'", 'attribute_value', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(!empty($exists)){
		return $attribute_value[0]['value_id'];
	}
	return '';

}

function getProductid($db, $KatalogNumber, $vparam1){
	$product_row = $db->fetch_row('katalognumber = ' . $KatalogNumber .", vparam1 = '" . $vparam1 ."'", 'product', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(!empty($exists)){
		return $product_row[0]['product_id'];
	}
	return '';
}

require_once('simplesql.class.php5');

$ini = parse_ini_file("web.ini");
while(list($key,$value) = each($ini)){
	if($key == 'hostName'){
		$hostName = $value;
	}
	else if($key == 'userName'){
		$userName = $value;
	}
	else if($key == 'password'){
		$password = $value;
	}
	else if($key == 'dbName'){
		$dbName = $value;
	}
}

$db = new simplesql($hostName,$userName,$password,$dbName);
$db->query("SET NAMES utf8");
//$db->showlog = true;
	/*
	$myarticles = file_get_contents('cache.txt');
	$objInfo = unserialize($myarticles);
	$arrInfo = toArray($objInfo);
	*/
	$mystock = file_get_contents('stock.txt');
	$objStock = unserialize($mystock);
	$arrStock = toArray($objStock);
	
	
	foreach($arrStock as $key=>$value){
		$ean = $value["Key"];
		$onstock = $value["Value"];
		echo $ean . "-" . $onstock . "<br />";
	}
	
	
	echo '</pre>';


?>
</body>
</html>